<?php
namespace App\Repositories;

use App\Models\AdminRole;

class AdminRoleRepository
{
    //添加角色
	public function addRole($request)
	{
		$res = AdminRole::create([
			'title'       => $request->title,
			'description' => $request->description,
			'status'      => $request->status
		]);
		return $res;
	}

    //获取全部角色
    public function getRoles()
    {
        return AdminRole::all();
    }

    public function getRolesExceptRoot()
    {
        return AdminRole::where('id', '<>', 1)->get();
    }

    //更新角色
    public function updateRole($request, $id)
    {
        $role = AdminRole::findOrFail($id);

        $role->title       = $request->title;
        $role->description = $request->description;
        $role->status      = $request->status;

        $role->save();
    }

    //删除角色
    public function deleteRole($id)
    {
        return AdminRole::find($id)->delete();
    }

    //批量删除
    public function deleteRoleLists($ids)
    {
        return AdminRole::whereIn('id',$ids)->delete();
    }

    /**
     * 获取角色菜单
     * @param  [type] $id 角色ID
     * @return [type]     [description]
     */
    public function getRoleMenus($id)
    {
        $menus = AdminRole::find($id)->menus()->pluck('title','id')->toArray();
        return $menus;
    }

    /**
     * 更新角色权限
     * @param  [type] $request [description]
     * @param  [type] $id      [description]
     * @return [type]          [description]
     */
    public function updateRolePermissions($request, $id)
    {
        return AdminRole::find($id)->menus()->sync($request->permissions);
    }

}